<template>
    <div>
      <div class="bg-white grid-content">
        <div style="overflow: hidden; padding-bottom:8px;">
          <div class="mytitle"><svg-icon icon-class="yjbb" class="myIcon" />油井基础数据</div>
          <div class="myButtons">
            <el-button @click="handleClick('', 'add')">新增</el-button>
          </div>
        </div>
        <!-- 表格 -->
        <el-table :data="tableData" :header-cell-style="{background:'#F5F7FA',color:'#909399'}" border stripe>
          <el-table-column align="center" type="index" label="序号" :index="indexMethod" width="60">
            </el-table-column>
            <el-table-column align="center" prop="jh" label="井号">
            </el-table-column>
            <el-table-column align="center" prop="bzid" label="班组ID">
            </el-table-column>
            <el-table-column align="center" prop="gqid" label="工区ID">
            </el-table-column>
            <el-table-column align="center" prop="jgid" label="机构ID">
            </el-table-column>
            <el-table-column align="center" prop="zkid" label="站库ID">
            </el-table-column>
            <el-table-column align="center" prop="jid" label="间ID">
            </el-table-column>
            <el-table-column align="center" prop="wybs" label="井唯一标识符">
            </el-table-column>
            <el-table-column align="center" prop="tyjm" label="通用井名">
            </el-table-column>
            <el-table-column align="center" prop="bzjm" label="标准井名">
            </el-table-column>
            <el-table-column align="center" prop="jlx" label="井类型">
            </el-table-column>
            <el-table-column align="center" prop="jb" label="井别">
            </el-table-column>
            <el-table-column align="center" prop="jx" label="井型">
            </el-table-column>
            <el-table-column align="center" prop="scfs" label="生产方式">
            </el-table-column>
            <el-table-column align="center" prop="bz" label="备注">
            </el-table-column>


            <el-table-column align="center" label="操作" width="140">
              <template slot-scope="scope">
                <el-button @click="handleClick(scope.row, 'check')" type="text" style="color: #2ec02e">查看</el-button>
                <el-button @click="handleClick(scope.row, 'edit')" type="text">编辑</el-button>
                <el-button @click="handleDelete(scope.row)" type="text" style="color: red">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        <!-- 分页器 -->
        <el-pagination align='right'
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNo"
            :page-sizes="[5,10, 20, 30, 40, 50]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
      </div>

      <!-- 弹出框 -->
      <el-dialog :title="myDialogTitle" :visible.sync="dialogFormVisible" :before-close="handleClose" custom-class="saveAsDialog">

        <el-form :inline="true" :model="yjjcsjForm" class="demo-form-inline" :rules="rules" ref="yjjcsjForm" :disabled="isInput" label-position="left" label-width="130px" >
          <!-- <el-divider content-position="left">基础信息</el-divider> -->
          <el-row>
              <el-col :span="12">
                <el-form-item label="井号:" prop="jh">
                  <el-input v-model="yjjcsjForm.jh" placeholder="请输入井号"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          <el-row>
              <el-col :span="12">
                <el-form-item label="所属班组:" prop="bzid">
                  <el-select v-model="yjjcsjForm.bzid" placeholder="请选择所属班组" filterable>
                    <el-option
                        v-for="(item,index) in bzList"
                        :key="item.bzid"
                        :label="item.bzmc"
                        :value="item.bzid"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="所属工区:" prop="gqid">
                  <el-select v-model="yjjcsjForm.gqid" placeholder="请选择所属工区" filterable>
                    <el-option
                        v-for="(item,index) in gqList"
                        :key="item.gqid"
                        :label="item.gqmc"
                        :value="item.gqid"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="所属机构:">
                  <el-select v-model="yjjcsjForm.jgid" placeholder="请选择所属机构" filterable>
                    <el-option
                        v-for="(item,index) in jgList"
                        :key="item.jgid"
                        :label="item.jgmc"
                        :value="item.jgid"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="所属站库:">
                  <el-select v-model="yjjcsjForm.zkid" placeholder="请选择所属站库" filterable>
                    <el-option
                        v-for="(item,index) in zkList"
                        :key="item.zkid"
                        :label="item.zkmc"
                        :value="item.zkid"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="所属间:">
                  <el-select v-model="yjjcsjForm.jid" placeholder="请选择所属间" filterable>
                    <el-option
                        v-for="(item,index) in jList"
                        :key="item.jid"
                        :label="item.jmc"
                        :value="item.jid"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="井唯一标识符:">
                  <el-input v-model="yjjcsjForm.wybs" placeholder="请输入井唯一标识符"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="通用井名:">
                  <el-input v-model="yjjcsjForm.tyjm" placeholder="请输入通用井名"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="标准井名:">
                  <el-input v-model="yjjcsjForm.bzjm" placeholder="请输入标准井名"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="井类型:">
                  <el-input v-model="yjjcsjForm.jlx" placeholder="请输入井类型"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="井别:">
                  <el-input v-model="yjjcsjForm.jb" placeholder="请输入井别"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="井型:">
                  <el-input v-model="yjjcsjForm.jx" placeholder="请输入井型"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="生产方式:">
                  <el-input v-model="yjjcsjForm.scfs" placeholder="请输入生产方式"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="备注:">
                  <el-input v-model="yjjcsjForm.bz" placeholder="请输入备注"></el-input>
                </el-form-item>
              </el-col>
            </el-row>

        </el-form>

        <div slot="footer" class="dialog-footer" v-show="!ischeck">
          <el-button @click="close('yjjcsjForm')">取 消</el-button>
          <el-button type="primary" @click="submitForm('yjjcsjForm')">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </template>

  <script>
  import axios from "axios";
  import { getToken } from "@/utils/auth";
  import zzjgApi from '@/api/jcsj/jcsy/zzjg';
  import cybzApi from '@/api/jcsj/jcsy/cybz';
  import gqApi from '@/api/jcsj/jcsy/gqjcsj';
  import zkApi from '@/api/jcsj/jcsy/zkjcsj';
  import jApi from '@/api/jcsj/jcsy/jianjcsj';
  import yjApi from '@/api/jcsj/jcsy/yjjcsj';

  export default {
    data() {
      return {
        // show:false,//隐藏序列
        pageNo: 1, //当前页码
        pageSize: 10, //每页显示数量
        total: 0, //总数量
        // table数据
        tableData: [],
        //当前页码
        currentPage: 1,
        //表单填报的数据
        yjjcsjForm: {
          jh:'',//井ID
          bzid:'',//班组ID
          gqid:'',//工区ID
          jgid:'',//机构ID
          zkid:'',//站库ID
          jid: '',//间ID
          wybs: '',//井唯一标识符
          tyjm: '',//通用井名
          bzjm: '',//标准井名
          jlx: '',//井类型
          jb: '',//井别
          jx: '',//井型
          scfs: '',//生产方式
          bz: '',//备注
        },
        //是否新增
        isadd: true,
        //验证规则
        rules: {
          jh: [
            { required: true, message: '请输入井号', trigger: 'blur' },
          ],
          bzid: [
            { required: true, message: '请选择所属班组', trigger: 'blur' },
          ],
        },
        //时间快捷选择
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now();
          },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
        //获取当前日期
        currentTime:'',
        //编辑框是否显示
        dialogFormVisible: false,
        //弹出框是否是查看
        ischeck: false,
        //控制查看时不可更改数据
        isInput:false,
        //弹出框标题
        myDialogTitle: '',

        jgList:[],//获取机构列表
        bzList:[],//获取班组列表
        gqList:[],//获取班组列表
        zkList:[],//获取班组列表
        jList:[],//获取班组列表
      }
    },
    created(){
      this.search();
    },
    mounted(){
      this.searchJg();
      this.searchBz();
      this.searchGq();
      this.searchZk();
      this.searchJ();
      this.filterTime();
    },
    methods: {
      /**
      * 查询列表
      */
      async search() {
        // this.pageNo = pageNo; //当前页码
        // this.pageSize = pageSize; //每页显示数量
        //发送查询请求
        let res = await yjApi.getList({pageNo:this.pageNo,pageSize:this.pageSize});
        //判断是否成功
        if(res.success){
          console.log(res.data);
          //赋值
          this.tableData = res.data.records;
          //总数量
          this.total = res.data.total;
        }
      },
      //查询班组列表
      async searchBz() {
        //发送查询请求
        let res = await cybzApi.getList2();
        //判断是否成功
        if(res.success){
          //赋值
          this.bzList = res.data;
          //总数量
        }
      },
      //查询机构列表
      async searchJg() {
        //发送查询请求
        let res = await zzjgApi.getList2();
        //判断是否成功
        if(res.success){
          //赋值
          this.jgList = res.data;
          //总数量
        }
      },
      //查询工区列表
      async searchGq() {
        //发送查询请求
        let res = await gqApi.getList2();
        //判断是否成功
        if(res.success){
          //赋值
          this.gqList = res.data;
          //总数量
        }
      },
      //查询站库列表
      async searchZk() {
        //发送查询请求
        let res = await zkApi.getList2();
        //判断是否成功
        if(res.success){
          //赋值
          this.zkList = res.data;
          //总数量
        }
      },
      //查询间列表
      async searchJ() {
        //发送查询请求
        let res = await jApi.getList2();
        //判断是否成功
        if(res.success){
          //赋值
          this.jList = res.data;
          //总数量
        }
      },
      onSubmit() {
        console.log('submit!');
      },
      //关闭对话框
      handleClose(done) {
        // 清空表单
        // this.$refs['yjjcsjForm'].resetFields();
        // 清空表单校验，避免再次进来会出现上次校验的记录
        this.$refs['yjjcsjForm'].clearValidate()
        done()

      },
        // 弹框关闭触发
      close(formName) {
        this.dialogFormVisible = false;
        // 清空表单
        // this.$refs[formName].resetFields();
        // 清空表单校验，避免再次进来会出现上次校验的记录
        this.$refs[formName].clearValidate()
      },

      submitForm(formName) {
        //表单验证
        this.$refs[formName].validate(async (valid) => {
            if (valid) {
                let res = null;
                console.log('111'+this.yjjcsjForm.jh)
                //判断ID是否为空
                if (this.isadd) {
                    console.log(this.yjjcsjForm)
                    //发送添加请求
                    res = await yjApi.add(this.yjjcsjForm);
                } else {
                    //发送修改请求
                    res = await yjApi.update(this.yjjcsjForm);
                }
                console.log(res)
                //判断是否成功
                if (res.success) {
                    this.$message.success(res.message);
                    //刷新
                    this.search();
                    //关闭窗口
                    this.dialogFormVisible = false;
                } else {
                  console.log(res)
                    this.$message.error(res.message);
                }
            }
        })
      },
      handleClick(row, handle) {
        // console.log(row.approveStatus);
        this.dialogFormVisible = true;
        if (handle == 'check') {
          this.myDialogTitle = '油井基础数据'
          this.ischeck = true
          this.isInput = true;// 1.表单禁用
          // this.yjjcsjForm = row
          this.$objCopy(row, this.yjjcsjForm);//把当前要编辑的数据复制到数据域，给表单回显
          this.$message({ message: '成功打开', type: 'success' });
        } else if (handle == 'edit') {
              this.myDialogTitle = '油井基础数据编辑'
              this.ischeck = false
              this.isInput = false
              this.isadd = false
              this.$objCopy(row, this.yjjcsjForm);//把当前要编辑的数据复制到数据域，给表单回显
        } else if (handle == 'add') {
          // this.yjjcsjForm ={}
          this.$resetForm("yjjcsjForm", this.yjjcsjForm); //清空表单数据
          this.myDialogTitle = '油井基础数据填报'
          this.isadd = true
          this.ischeck = false
          this.isInput = false
        }
      },

      //删除行数据
      async handleDelete(row) {
        // var param={ jh: row.jh, bzid: row.bzid }

        // let confirm = await this.$myconfirm("确定要删除该数据吗?");
        // console.log(confirm)
        // if (confirm) {
        //   axios({
        //     url: "http://47.108.251.250:3296/api/jcJJcsj/delete",
        //     method: "delete",
        //     headers: {
        //       "token": getToken(),
        //       "Content-Type": "application/json",
        //     },
        //     data: param,
        // }).then(
        //     (response) => {
        //     if (response.data.success === true) {
        //       console.log(response.data);
        //       this.$message.success(response.data.message);
        //       //刷新
        //       this.search();
        //     }else{
        //       this.$message.error('删除失败');
        //     }
        //   },
        //   (error) => {
        //     console.log(error);
        //     this.$message.error(response.data.message);
        //   }
        //   )

        //发送删除请求
        let res = await yjApi.delete({ jh: row.jh, bzid: row.bzid });
        console.log(res)
        //判断是否成功
        if (res.success) {
            //成功提示
            this.$message.success(res.message);
            //刷新
            this.search();
        } else {
            //失败提示
            this.$message.error(res.message);
        }
        // }

      },
        //批量删除
      handleSelectionChange(val){
        this.multipleSelection = val;
        console.log(this.multipleSelection);
      },
      async handleBDelete() {
        //判断是否可以删除
        //确认是否删除
        let confirm = await this.$myconfirm("确定要删除该数据吗?");
        console.log(confirm)
        var ids = []
        if (confirm) {
            for(let j=0 ; j<this.multipleSelection.length; j++){
                if (this.multipleSelection[j].approveStatus == 2) {
                    //提示不能删除
                    console.log(this.multipleSelection[j].approveStatus)
                    this.$message({ message: '有已通过数据不可删除！', type: 'error' });
                    return;

                }
                ids[j] = this.multipleSelection[j].id
            }
            console.log("ids",ids)
            // const ids = ids.toString();
            // console.log("ids",ids)
            //发送删除请求
            console.log('111')
            let res = await xcwxApi.Bdelete(ids);
            console.log('222')
            console.log(res)
            //判断是否成功
            if (res.success) {
            //成功提示
            this.$message.success(res.message);
            //刷新
            this.search();
            } else {
            //失败提示
            this.$message.error(res.message);
            }
        }
      },

      /*** 当每页数量发生变化时触发该事件 */
      handleSizeChange(size) {
        console.log(size);
        this.pageSize = size;
        //将每页显示的数量交给成员变量
        this.search(this.pageNo, size);
      },
      /*** 当页码发生变化时触发该事件 */
      handleCurrentChange(page) {
        console.log(page);
        this.pageNo = page;
        //调用查询方法
        this.search(page, this.pageSize);
      },
      // 序号计算
      indexMethod (index) {
      // index默认在0开始，这里+1
        return index + 1 + (this.pageNo - 1) * this.pageSize
      },
      // 获取当前日期
      filterTime() {
        var date = new Date();
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? '0' + m : m;
        var d = date.getDate();
        d = d < 10 ? '0' + d : d;
        this.currentTime = y + '-' + m + '-' + d;
        // console.log(typeof(new Date(this.currentTime)));
      },
    }
  }
  </script>

  <style lang="scss" scoped>
  // 标题
  .mytitle {
    font-size: large;
    padding-left: 10px;
    margin-bottom: 16px;
    float: left;
  }

  //按钮组
  .myButtons {
    float: right
  }

  //格子的样式
  .bg-white {
    background: #ffffff;
  }

  .grid-content {
    padding: 10px;
    margin-bottom: 10px;
    border-radius: 4px;
  }

  .calendar ::v-deep .el-button-group::before {
    display: none;
  }

  .calendar ::v-deep .el-button-group::after {
    display: none;
  }

  // 分割线样式
  .el-divider--horizontal {
    display: block;
    height: 2px;
    width: 100%;
    margin: 24px 0;
  }

  // 表单各项的样式
  .el-form-item {
    margin: 15px 10px 15px 10px;
  }

  //表单禁用字体样式
  .el-input.is-disabled ::v-deep .el-input__inner {
      color: #606266;
    }
  //对话框宽度
  // .customWidth{
  //         width:80%;
  //     }
  ::v-deep .saveAsDialog {
    width: 50% !important;
  }

  ::v-deep .el-table .cell{overflow: unset}
  //日期选择框宽度
  .el-date-editor.el-input, .el-date-editor.el-input__inner { width: 200px; }

  .el-input {
    width: 200px;
  }
  </style>
